using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	#endregion
	public class ThongTinCongTyRepository
	{
		public List<ThongTinCongTy> SelectAll()
		{
			return DataContext.Instance.ThongTinCongTy.ToList();
		}

		public ThongTinCongTy SelectByID(int id)
		{
			return DataContext.Instance.ThongTinCongTy.FirstOrDefault(item => item.ID == id );
		}

		public List<ThongTinCongTy> SelectPaging(int page, int pageSize)
		{
			var lstItem = DataContext.Instance.ThongTinCongTy.ToList();
			if (pageSize > 0)
			{
				int startIndex, itemCount;
				startIndex = (page - 1) * pageSize;
				itemCount = pageSize;
				if (startIndex + itemCount > lstItem.Count)
					itemCount = lstItem.Count - startIndex;
				lstItem = lstItem.GetRange(startIndex, itemCount);
			}
			return lstItem;
		}

		public bool Insert(ThongTinCongTy obj)
		{
			try
			{
				DataContext.Instance.ThongTinCongTy.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.ThongTinCongTy.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.ThongTinCongTy.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			return DataContext.Instance.ThongTinCongTy.Count();
		}


		public int GetTotalPage()
		{
            return 1;
		}


		public List<ThongTinCongTy> RetrieveByID(int id)
		{
			return (from item in DataContext.Instance.ThongTinCongTy where  item.ID == id  select item).ToList();
		}

	}
}